<?php
/**
 *
 *
 * @author: mgli<mgli@kaikeba.com>
 * @date: 2021/3/5
 * @version: 1.0
 * @copyright: mgli@kaikeba.com @ 2021
 * @filename: TaskMemberController.php
 */

namespace App\Admin\Controllers;

use App\Admin\RenderAble\TaskTable;
use App\Admin\Repositories\MemberTask;
use App\Admin\Repositories\OrgDepartment;
use App\Admin\Repositories\OrgDepartmentOwner;
use App\Admin\Repositories\OrgTeamOwner;
use App\Admin\Repositories\TeamMember;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Illuminate\Support\Facades\Auth;

class TaskMemberController extends AdminController
{
    protected $title = "任务管理";

    public function index(Content $content)
    {
        return $content
            ->header('任务管理')
            ->description('人员视角')
            ->body($this->grid());
    }

    public function grid()
    {
        return Grid::make(new TeamMember(['department', 'team']), function (Grid $grid) {
            if ($this->isAdmin()){
                $grid->model()
                    ->where('status', \App\Models\TeamMember::MEMBER_STATUS_ON)
                    ->orderBy('status', 'desc')
                    ->orderBy('member_id', 'desc');
            }else{
                $owner_department_list = app(OrgDepartmentOwner::class)->getDepartmentListByOwnerUid(Auth::guard('admin')->id());
                $department_id_list = $team_id_list = [];
                if ($owner_department_list){
                    $department_id_list = $owner_department_list->pluck('department_id')->toArray();
                }
                $owner_team_list = app(OrgTeamOwner::class)->getTeamListByOwnerUidAndDepartmentIds(Auth::guard('admin')->id(), $department_id_list);
                if ($owner_team_list){
                    $team_id_list = $owner_team_list->pluck('team_id')->toArray();
                }

                $grid->model()
                    ->where('status', \App\Models\TeamMember::MEMBER_STATUS_ON)
                    ->whereIn('team_id', $team_id_list)
                    ->orderBy('status', 'desc')
                    ->orderBy('member_id', 'desc');
            }

            unset($GLOBALS['cnt_list']);
            $grid->column('member_id', '成员ID')->sortable();
            $grid->column('member_name', '成员姓名');

            $grid->column('task_progress', '任务处理进度')
                ->width(150)
                ->display(function (){
                    $list = app(MemberTask::class)->getTaskCountByStatus($this->member_id);
                    $list = collect($list)->keyBy('status')->toArray();
                    foreach (\App\Models\MemberTask::$status_list as $status => $name){
                        $GLOBALS['cnt_list'][$this->member_id][$status] = isset($list[$status]) ? $list[$status]['cnt'] : 0;
                    }

                    $current_cnt_list = $GLOBALS['cnt_list'][$this->member_id];
                    unset($current_cnt_list[\App\Models\MemberTask::TASK_STATUS_COMPLETE]);
                    $total_task_cnt = array_sum($current_cnt_list);
                    if ($total_task_cnt == 0){
                        return 0;
                    }
                    return ceil($current_cnt_list[\App\Models\MemberTask::TASK_STATUS_ING]/$total_task_cnt*100);

                })->progressBar()->help('计算公式：处理中/(待处理+处理中+已暂停)*100%');

            $grid->column('ready_task_num', '未开始任务数')
                ->display(function (){
                    return $GLOBALS['cnt_list'][$this->member_id][\App\Models\MemberTask::TASK_STATUS_READY];
                })
                ->expand(function (){
                    return TaskTable::make(['status' => \App\Models\MemberTask::TASK_STATUS_READY, 'progress' => null]);
                })->label('#eee');

            $grid->column('ing_task_num', '执行中任务数')
                ->display(function (){
//                    return app(MemberTask::class)->getTaskCountByMemberIdAndStatus($this->member_id, \App\Models\MemberTask::TASK_STATUS_ING);
                    return $GLOBALS['cnt_list'][$this->member_id][\App\Models\MemberTask::TASK_STATUS_ING];
                })
                ->expand(function (){
                    return TaskTable::make(['status' => \App\Models\MemberTask::TASK_STATUS_ING, 'progress' => null]);
                })->label('#eee');

            $grid->column('pause_task_num', '已暂停任务数')
                ->display(function (){
//                    return app(MemberTask::class)->getTaskCountByMemberIdAndStatus($this->member_id, \App\Models\MemberTask::TASK_STATUS_PAUSE);
                    return $GLOBALS['cnt_list'][$this->member_id][\App\Models\MemberTask::TASK_STATUS_PAUSE];
                })
                ->expand(function (){
                    return TaskTable::make(['status' => \App\Models\MemberTask::TASK_STATUS_PAUSE, 'progress' => null]);
                })->label('#eee');

            $grid->column('delay_task_num', '延期任务数')
                ->display(function (){
                    return app(MemberTask::class)->getTaskCountByMemberIdAndProgress($this->member_id, \App\Models\MemberTask::TASK_PROGRESS_DELAY);
                })
                ->expand(function (){
                    return TaskTable::make(['status' => null, 'progress' => \App\Models\MemberTask::TASK_PROGRESS_DELAY]);
                })->label('#eee');

            $grid->column('delay_task_num', '最近完成任务数')
                ->display(function (){
                    return app(MemberTask::class)->getCurrentCompleteTaskCount($this->member_id);
                })->help('七天内完成的任务')
                ->expand(function (){
                    return TaskTable::make(['status' => \App\Models\MemberTask::TASK_STATUS_COMPLETE, 'progress' => null]);
                })->label('#eee');

            $grid->column('done_task_num', '已完成任务数')
                ->display(function (){
//                    return (int) app(MemberTask::class)->getTaskCountByMemberIdAndStatus($this->member_id, \App\Models\MemberTask::TASK_STATUS_COMPLETE);
                    return $GLOBALS['cnt_list'][$this->member_id][\App\Models\MemberTask::TASK_STATUS_COMPLETE];
                });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();

                if ($this->isAdmin()){
                    $department_list = app(OrgDepartment::class)->getOptionList();
                }else{
                    $owner_team_list = app(OrgTeamOwner::class)->getTeamListByOwnerUid(Auth::guard('admin')->id());
                    $department_id_list = $owner_team_list->pluck('department_id')->unique()->toArray();
                    $department_list = app(OrgDepartment::class)->getOptionListByDepartmentIds($department_id_list);
                }

                $filter->equal('department_id', '部门')
                    ->width(2)
                    ->select($department_list)
                    ->load('team_id', '/api/team');
                $filter->equal('team_id', '小组')
                    ->width(2)
                    ->select();
            });


            $grid->column('team.name', '所属小组')->label();
            $grid->column('department.name', '所属部门')->label('success');

            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->showFilterButton();
        });
    }
}